var ns;
var app = new Vue({
    el: '#app',
    data: {
        passwordtype: 0,
        oldPassword:'',
        oldcorrect:0, //旧密码是否正确  1：正确
        form: {
            id: '',
            password: '',
            newpassword: '',
        },
        LoginName:'',
        NickName:'',
        pointMsg:'',

    },
    methods: {
        get_data: function() {
            var that = this;
            var opt = {
                action: 'geteditpasswordparams',
            };
            ns.post(opt, function(succeed, data, err) {
                if(succeed){
                    that.LoginName=data.LoginName;
                    that.NickName=data.NickName;
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                      });
                }
            });

        },
        inputNum:function(){
            var that=this;
            that.pointMsg='';
        },
        onChangeNum:function(){
            var that=this;
            var opt = {
                action: 'checkoldpassword',
                Password: that.oldPassword,
            };
            ns.post(opt, function(succeed, data, err) {
                if (succeed) {
                    that.oldcorrect=1;
                }else{
                    that.pointMsg="原密码输入不正确"
                }
            });
        },
        inputpassword:function(){
            var that=this;
            that.pointMsg='';
        },
        onChangePassword:function(){
            var that=this;
            if(that.form.password!==that.form.newpassword){
                that.pointMsg="两次密码输入不正确"
            }
        },
       
        open_savePassword: function() {
            var that = this;
            if(that.oldcorrect==1)
            {
              // alert(that.form.id);
              //获取输入的密码，调用接口保存密码
              if (that.form.password == '') {
                  api.toast({
                      msg: '请输入新密码',
                      duration: 2000,
                      location: 'bottom'
                  });
                  return;
              }
              if (that.form.newpassword == '') {
                  api.toast({
                      msg: '请再次输入新密码',
                      duration: 2000,
                      location: 'bottom'
                  });
                  return;
              }
              if (that.form.newpassword != that.form.password) {
                  api.toast({
                      msg: '两次输入的密码不一致',
                      duration: 2000,
                      location: 'bottom'
                  });
                  return;
              }
              var opt = {
                  action: 'savenewpassword4form',
                  OldPassword:that.oldPassword,
                  Newpassword:that.form.password,
                  RePassword: that.form.newpassword
              };
              ns.post(opt, function(succeed, data, err) {
                  if (succeed) {
                      //成功
                      api.toast({
                          msg: '操作成功',
                          duration: 2000,
                          location: 'bottom'
                      });

                      api.removePrefs({
                          key: 'uid'
                      });
                      api.removePrefs({
                          key: 'busid'
                      });
                      api.removePrefs({
                          key: 'familyuid'
                      });

                      api.removePrefs({
                          key: 'isfirstchangepwd'
                      });
                      //操作成功后跳转到登录界面
                      setTimeout(function() {
                          ns.openWin('login_frm', '', {
                              id: '',
                          });
                      }, 200);
                  } else if (err) {
                      api.toast({
                          msg: err,
                          duration: 2000,
                          location: 'bottom'
                      });
                  }
              });
            }
            else {
              api.toast({
                  msg: '原密码不正确',
                  duration: 2000,
                  location: 'bottom'
              });
            }

        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.id = api.pageParam.id;
    app.get_data();
};
